/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbf;

import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import koneksi.Koneksi;

/**
 *
 * @author rosi
 */
public class QueryDBF {

    String fileName = "E:/MS_PRODU.DBF";
    ArrayList<Object> kolom = new ArrayList<Object>();

    public void bacaFile() throws FileNotFoundException, IOException {

        try {
            FileInputStream inputStream = new FileInputStream(fileName);
            DBFReader reader = new DBFReader(inputStream);

            //baca nama kolom
            int jmlKolom = reader.getFieldCount();
            for (int i = 0; i < jmlKolom; i++) {

                DBFField field = reader.getField(i);
                System.out.print(field.getName() + "\t");
                kolom.add(field.getName());
            }
            System.out.println("\n");

            //baca isi kolom
            Object[] rowObjects;
            while ((rowObjects = reader.nextRecord()) != null) {
                for (int i = 0; i < rowObjects.length; i++) {
                    System.out.print(rowObjects[i] + "  ");
                }
                System.out.println();
            }

            inputStream.close();
        } catch (DBFException e) {
            System.out.println(e.getMessage());
        }
    }

    public void convert() {
//        String namaTabel = fileName;
        String namaKolom = "";
        String kolom1, kolom2, kolom3, kolom4, kolom5, kolom6,kolom7;
//        try{
        
//            for (Object nama : kolom) {
        for (int i = 0; i <kolom.size(); i++) {
            kolom1 = kolom.get(i).toString();
                System.out.println(kolom1);
                
            }
        
        
        
//            Connection conn = Koneksi.open();
//            Statement state = conn.createStatement();
//            String query = "create table " + namaTabel
//                    +"("+(namaKolom)
//                    +")";
//            state.execute(query);
//            System.out.println(namaKolom);

//        } catch (SQLException ex) {
//            Logger.getLogger(QueryDBF.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (FileNotFoundException ex) {
//            Logger.getLogger(QueryDBF.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(QueryDBF.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }
}
